package com.xiefengf.es;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.annotations.Query;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author xiefengf
 * @version 1.0
 * @date 2024/3/25 11:26
 *
 * 接口关系：
 * ElasticsearchRepository --> ElasticsearchCrudRepository --> PagingAndSortingRepository --> CrudRepository
 *
 * 我们是通过继承ElasticsearchRepository来完成基本的CRUD及分页操作的，和普通的JPA没有什么区别。
 * ElasticSearchTemplate更多是对ElasticsearchRepository的补充，里面提供了一些更底层的方法。
 */
@Repository
public interface ProductMapper extends ElasticsearchRepository<Product,Long> {

    /**
     * 分词查询category
     * @param param1
     * @param pageable
     * @return
     */
    @Query("{\n" +
            "    \"match\": {\n" +
            "        \"category\": \"?0\"\n" +
            "    }\n" +
            "}")
    Page<Product> findByCategory(String param1, Pageable pageable);

    /**
     * 多查询条件查询数据
     * @param param1
     * @param param2
     * @param param3
     * @param param4
     * @return
     */
    @Query("{\n" +
            "    \"bool\": {\n" +
            "        \"should\": [\n" +
            "            {\n" +
            "                \"match\": {\n" +
            "                    \"category\": \"?0\"\n" +
            "                }\n" +
            "            },\n" +
            "            {\n" +
            "                \"match_phrase\": {\n" +
            "                    \"title\": \"?1\"\n" +
            "                }\n" +
            "            }\n" +
            "        ],\n" +
            "        \"filter\": {\n" +
            "            \"range\": {\n" +
            "                \"price\": {\n" +
            "                    \"gt\": ?2,\n" +
            "                    \"lt\": ?3\n" +
            "                }\n" +
            "            }\n" +
            "        }\n" +
            "    }\n" +
            "}")
    List<Product> findByConditions(String param1,String param2,Double param3,Double param4);

    /**
     * 根据id集合查询数据
     * @param ids
     * @return
     */
    @Query("{\n" +
            "    \"ids\": {\n" +
            "        \"values\": ?0\n" +
            "    }\n" +
            "}")
    List<Product> getByIds(List<String> ids);

}
